package com.mh.www.mh_frame.presenter.present.impl;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.mh.www.mh_frame.model.bean.UserBean;
import com.mh.www.mh_frame.network.RetrofitFactory;
import com.mh.www.mh_frame.presenter.iview.LoginView;
import com.mh.www.mh_frame.presenter.present.LoginPresent;
import com.mh.www.mh_frame.presenter.present.base.BasePresenterImpl;
import com.mh.www.mh_frame.util.OkHttpUtil;
import com.mh.www.mh_frame.util.okhttp.callback.StringCallback;
import com.orhanobut.logger.Logger;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;


/**
 * @author think
 * @version [版本号, 2018/1/22]
 * @see [相关类/方法]
 * @since [V1]
 */

public class LoginPresentImpl extends BasePresenterImpl<LoginView> implements LoginPresent {
    @Override
    public void login() {
        if (TextUtils.isEmpty(mView.getUsername())){
            mView.usernameIsEmpty();
            return;
        }
        if (TextUtils.isEmpty(mView.getPassword())){
            mView.passwordIsEmpty();
            return;
        }

    }
    @Override
    public void clear() {
        mView.clearPassword();
        mView.clearUsername();
    }

    @Override
    public void getLogin2(String userName, String pwd) {
//        OkHttpUtil.post().url("http://112.29.132.167:3302/pcmserver/j_spring_security_check")
//                .addParams("j_username",mView.getUsername())
//                .addParams("j_password",mView.getPassword())
//                .build().execute(new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                Logger.e("onNext"+"---"+e.getMessage());
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                getsumm3("admin");
//                Logger.e("onNext"+"---"+response);
//            }
//        });
        RetrofitFactory.getInstence().API()
                .getLogin(userName,pwd)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {

                    @Override
                    public void onSubscribe(Disposable d) {
//                        Logger.e("onSubscribe"+d.toString());
                    }

                    @Override
                    public void onNext(String bean) {
                        Logger.e("onNext"+"---"+bean+"--");
                        if (bean.contains("againLogin")){
                            mView.showMsg("账号或密码错误");
                        }else{
                            getSummary2("admin");
//                            getsumm3("admin");
                            Gson gson = new Gson();
                            UserBean userBean = gson.fromJson(bean, UserBean.class);
                            Logger.e("bean",bean);
                            Logger.e("userBean"+"----------"+userBean.getEmp_name()+"-----"+userBean.getUsername());

                            mView.navigateToHome();
                            mView.setData(userBean);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        Logger.e("onError",e.getMessage()+"--");
                    }

                    @Override
                    public void onComplete() {
//                        Logger.e("完成","onCompleted");
                    }
                });
    }
    public void getSummary2(String userid) {
        RetrofitFactory.getInstence().API()
                .getMeetingSummary2(userid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String meetingSummaryBean) {
                        Logger.e("onNext"+"---"+meetingSummaryBean);
//                            mView.setData(meetingSummaryBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Logger.e("onError"+e.getMessage()+"--");
                    }
                    @Override
                    public void onComplete() {
                    }
                });
    }
    public void getsumm3(String userid){
        OkHttpUtil.post().url("http://112.29.132.167:3302/pcmserver/user/checkUserId")
                .addParams("userId",userid)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Logger.e("onNext"+"---"+e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                Logger.e("onNext"+"---"+response);
            }
        });
    }
}
